package com.iot.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.Company;
import com.iot.manage.result.Code;
import com.iot.manage.result.Result;
import com.iot.manage.service.CompanyService;
import com.iot.manage.service.TokenService;
import com.iot.manage.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 厂家管理控制层
 *
 * @author zhuzhi
 */
@RestController
@RequestMapping("/api/private/v1")
public class CompanyController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CompanyService companyService;

    /**
     * 查询厂家信息列表
     *
     * @param token
     * @param query 模糊查询关键字
     * @param pagenum 当前页
     * @param pagesize 每页条数
     * @return
     */
    @GetMapping("/findCompanyList")
    public Result findCompanyList(
            @RequestHeader("token") String token,
            @RequestParam("query") String query,
            @RequestParam("pagenum") int pagenum,
            @RequestParam("pagesize") int pagesize) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<Company> companyList;
        int companyTotal;
        try{
            companyList = companyService.findCompanyList(query,pagenum,pagesize);
            companyTotal = companyService.countCompanyList(query);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",companyTotal);
        map.put("pagenum",pagenum);
        map.put("companyList",companyList);
        return result.build(map, Code.S200);
    }

    /**
     * 新增厂家信息
     *
     * @param token
     * @param params 厂家信息参数
     * @return
     */
    @PostMapping("/addCompanyInfo")
    public Result addCompanyInfo(
            @RequestHeader("token") String token,
            @RequestBody JSONObject params) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if(null != findCompanyInfoByIdOrCode(token,params.getString("companyCode")).getData()){
                return result.build(null, Code.D411);
            }
            params.put("companyId", UUID.randomUUID().toString().replace("-", ""));
            params.put("companyArg", StringUtils.replaceBlank(params.get("companyArg").toString()));
            companyService.addCompanyInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 根据厂家ID或厂家标识查询厂家信息
     *
     * @param token
     * @param params 厂家ID或厂家标识
     * @return
     */
    @GetMapping("/findCompanyInfoByIdOrCode/{params}")
    public Result findCompanyInfoByIdOrCode(
            @RequestHeader("token") String token,
            @PathVariable("params") String params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        Company company;
        try{
            company = companyService.findCompanyInfoByIdOrCode(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(company, Code.S200);
    }

    /**
     * 修改厂家信息
     *
     * @param token
     * @param params 厂家信息参数
     * @return
     */
    @PutMapping("/updateCompanyInfo/{oldCompanyCode}")
    public Result updateCompanyInfo(
            @RequestHeader("token") String token,
            @PathVariable("oldCompanyCode") String oldCompanyCode,
            @RequestBody JSONObject params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if (!(oldCompanyCode.equals(params.get("companyCode")))){
                if(null != findCompanyInfoByIdOrCode(token,params.getString("companyCode")).getData()){
                    return result.build(null, Code.D411);
                }
            }
            params.put("companyArg", StringUtils.replaceBlank(params.get("companyArg").toString()));
            companyService.updateCompanyInfo(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     *删除厂家信息
     *
     * @param token
     * @param companyId 厂家ID
     * @return
     */
    @DeleteMapping("/removeCompanyInfo/{companyId}")
    public Result removeCompanyInfo(
            @RequestHeader("token") String token,
            @PathVariable("companyId") String companyId) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            companyService.removeCompanyInfo(companyId);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }
}
